class Node:
    def __init__(self):
        self.data = None
        self.head = None
        self.tail = None
        self.next = None
        self.prev = None


class List:
    def __init__(self, length=0):
        self.length = length
        self.head = Node()
        self.tail = Node()

        self.head.next = self.tail
        self.tail.prev = self.head

    def append(self, data):
        new_node = Node()
        new_node.data = data

        p_node = self.tail.prev
        p_node.next = new_node
        new_node.prev = p_node
        new_node.next = self.tail
        self.tail.prev = new_node
        self.length += 1

    def remove(self):
        if self.length > 0:
            a=self.tail.prev.prev
            a.next=self.tail
            self.tail.prev=a
            self.length -= 1

    def insert(self,pos,data):
        assert pos>=0 and pos<=self.length
        a=self.head
        while pos>0:
            a=a.next
            pos-=1

        new_node=Node()
        # b=a.next


        new_node.data=data

        new_node.prev = a
        new_node.next = a.next

        a.next.prev = new_node
        a.next = new_node
        # new_node.next=b
        # b.prev=new_node

        self.length += 1


    def update(self, pos, data):
        newNode= Node()
        newNode.data = data

        assert pos<self.length and pos>= 0
        tmpPtr = self.head
        while pos>0:
            tmpPtr = tmpPtr.next
            pos -= 1

        tmpPtr.data = newNode.data

    def __len__(self):
        return self.length














